package whu.cn.whushare.ui.main.known.adapter;

import java.util.List;

import whu.cn.whushare.R;
import whu.cn.whushare.bean.User;
import whu.cn.whushare.common.ImageWorkerManager;
import whu.cn.whushare.ui.main.known.bean.AnswerWrapBean;
import whu.cn.whushare.ui.main.known.bean.CommentWrapper;
import whu.cn.whushare.util.Utils;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.androidplus.util.ViewMapping;
import com.androidplus.util.ViewMappingUtil;

public class CommentListAdapter extends BaseAdapter {
	private Context mContext;
	private List<CommentWrapper> mCommentWrapper;

	public CommentListAdapter(Context mContext, List<CommentWrapper> mAnswerDetail) {
		this.mContext = mContext;
		this.mCommentWrapper = mAnswerDetail;
	}

	@Override
	public int getCount() {
		return mCommentWrapper.size();
	}

	@Override
	public CommentWrapper getItem(int position) {
		return mCommentWrapper.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder viewHolder;
		if (convertView == null) {
			convertView = LayoutInflater.from(mContext).inflate(R.layout.layout_answer_comment_item, null);
			viewHolder = new ViewHolder();
			ViewMappingUtil.mapView(viewHolder, convertView);
			convertView.setTag(viewHolder);
		} else {
			viewHolder = (ViewHolder) convertView.getTag();
		}
		CommentWrapper commentWrapper = mCommentWrapper.get(position);
		viewHolder.content.setText(commentWrapper.comment.content);

		User user = commentWrapper.user;
		viewHolder.answerName.setText(user.getNickName() + "");
		if (!TextUtils.isEmpty(user.getAvatar())) {
			ImageWorkerManager.getInstance(mContext).loadCommonImage(Utils.getPhotoUrl(user.getAvatar()),
					viewHolder.answerAvatar, R.drawable.avatar, R.drawable.avatar);
		}
		return convertView;
	}

	class ViewHolder {
		@ViewMapping(id = R.id.avatar)
		private ImageView answerAvatar;
		@ViewMapping(id = R.id.user_name)
		private TextView answerName;
		@ViewMapping(id = R.id.agree)
		private TextView agreeCount;
		@ViewMapping(id = R.id.comment_content)
		private TextView content;
	}

}
